public class MySingleList {
    class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void CreateListNode() {
        ListNode Node1 = new ListNode(12);
        ListNode Node2 = new ListNode(23);
        ListNode Node3 = new ListNode(34);
        ListNode Node4 = new ListNode(45);

        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;


        this.head = Node1;
    }

    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.println(cur.val + " ");
            cur = cur.next;
        }

    }

    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void FirstAdd(int val) {
        ListNode Node = new ListNode(val);
        Node.next = head;
        head = Node;
    }

    public void LastAdd(int val) {
        ListNode Node = new ListNode(val);
        if (head == null) {
            head = Node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = Node;
    }

    private void indexCheck(int index) throws indexNotLegal {
        if (index < 0 || index > size()) {
            throw new indexNotLegal("index不合法");
        }
    }

    public void addIndex(int index, int val) {
        try {
            indexCheck(index);
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
        if (index == 0) {
            ListNode Node = new ListNode(val);
            Node.next = head;
            head = Node;
            return;
        }
        int ret = size();
        if (index == ret) {
            ListNode Node = new ListNode(val);
            if (head == null) {
                head = Node;
                return;
            }
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = Node;
            return;
        }
        ListNode Node = new ListNode(val);
        ListNode cur = this.head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        Node.next = cur.next;
        cur.next = Node;

    }

    public boolean contains(int value) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == value) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int value) {
        if(head==null){
            return;
        }
        if(head.val==value){
            head=head.next;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == value) {
                ListNode Del = cur.next;
                cur.next = Del.next;
                return;
            }
            cur = cur.next;
        }

    }

    public void removeAll(int value){
        if(head==null){
            return;
        }
        ListNode cur=head.next;
        ListNode pre=head;

        while (cur!=null){
            if(cur.val==value){
                pre.val=cur.val;
            }
            else{
                pre=pre.next;
            }
            cur=cur.next;

        }
        if(head.val==value){
            head=head.next;
        }
    }

    public void clear(){
        head=null;
    }

}